package com.susu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class NewController {
    @Autowired
    public RedisTemplate redisTemplate;

    @GetMapping("/")
    public String index(){
        return "index";
    }

    @GetMapping("detail/{id}")
    public String new1(@PathVariable Integer id, Model model){
        redisTemplate.opsForHash().increment("new"+id,"read",1);
        Integer o = (Integer) redisTemplate.opsForHash().get("new"+id,"read");
        model.addAttribute("read",o);
        return "new"+id;
    }

    @ResponseBody
    @GetMapping("/addZan/{id}/{type}")
    public Integer addZan(@PathVariable Integer id,@PathVariable Integer type){
        redisTemplate.opsForHash().increment("new"+id,"zan",type);
        Integer countZan = (Integer) redisTemplate.opsForHash().get("new" + id, "zan");
        return countZan;
    }
}
